package com.atguigu.msmservice.service.impl;

//import com.alibaba.fastjson.JSONException;
//import com.atguigu.msmservice.service.Msmservice;
//import com.atguigu.msmservice.utils.RandomUtils;
//import com.atguigu.msmservice.utils.TencentUtils;
//import com.github.qcloudsms.SmsSingleSender;
//import com.github.qcloudsms.SmsSingleSenderResult;
//import com.github.qcloudsms.httpclient.HTTPException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.concurrent.TimeUnit;
//
//import com.atguigu.msmservice.service.Msmservice;
//import com.atguigu.msmservice.utils.RandomUtils;
//import com.atguigu.msmservice.utils.TencentUtils;
//import com.github.qcloudsms.SmsSingleSender;
//import com.github.qcloudsms.SmsSingleSenderResult;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
import com.atguigu.msmservice.service.Msmservice;
import com.atguigu.msmservice.utils.RandomUtils;
import com.atguigu.msmservice.utils.TencentUtils;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

////@Service
////public class MsmserviceImpl implements Msmservice {
////    @Override
////    public String send(String phone) {
////        return null;
////    }
////    //发送短信的方法
////    @Override
////    public boolean send(HashMap<String, Object> param, String phone) {
////
////        //创建短信发送对象
////        SmsSingleSender ssender = new SmsSingleSender(TencentUtils.APPID, TencentUtils.APPKEY);
////
////        SmsSingleSenderResult result = ssender.sendWithParam(TencentUtils.NATIONCODE, phoneNum,
////                TencentUtils.TEMPLATECODE, params, TencentUtils.SIGN, "", "");
////
//////        //验证是否发送成功，发送成功将验证码返回 等待用户输入从而进行数据验证
//////        //{"result":0,"errmsg":"OK","ext":"","sid":"2433:22806866016300548263905129","fee":1,"isocode":"CN"}
//////        if (result.result == 0) {
//////            //发送成功 把验证码存入redis 并设置有效期5分钟
//////            redisTemplate.opsForValue().set(phoneNum, params[0], 5, TimeUnit.MINUTES);
//////            return params[0];
//////        }
////        return false;
////    }
//
//
//    @Service
//    public class MsmServiceImpl implements Msmservice {
//
//        @Autowired
//        private RedisTemplate<String, String> redisTemplate;
//
//        @Override
//        public String send(String phoneNum) {
//
//            //判断redis中是否有验证码或有效期未过
//            String code = redisTemplate.opsForValue().get(phoneNum);
//            if (!StringUtils.isEmpty(code)) {
//                return code;
//            }
//
//            // 如果没有则进行腾讯云短信发送
//            //生成6位随机验证码(验证码参数需要数组对象) 可以随意给出6为数字 我这里使用随机数
//            String[] params = {RandomUtils.getRandom()};
//
//            try {
//                //创建短信发送对象
//                SmsSingleSender ssender = new SmsSingleSender(TencentUtils.APPID, TencentUtils.APPKEY);
//
//                SmsSingleSenderResult result = ssender.sendWithParam(TencentUtils.NATIONCODE, phoneNum,
//                        TencentUtils.TEMPLATECODE, params, TencentUtils.SIGN, "", "");
//
//                //验证是否发送成功，发送成功将验证码返回 等待用户输入从而进行数据验证
//                //{"result":0,"errmsg":"OK","ext":"","sid":"2433:22806866016300548263905129","fee":1,"isocode":"CN"}
//                if (result.result == 0) {
//                    //发送成功 把验证码存入redis 并设置有效期5分钟
//                    redisTemplate.opsForValue().set(phoneNum, params[0], 5, TimeUnit.MINUTES);
//                    return params[0];
//                }
//
//            } catch (HTTPException e) {
//                // HTTP 响应码错误
//                e.printStackTrace();
//            } catch (JSONException e) {
//                // JSON 解析错误
//                e.printStackTrace();
//            } catch (IOException e) {
//                // 网络 IO 错误
//                e.printStackTrace();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return "";
//        }
//    }
////}
@Service
public class MsmserviceImpl implements Msmservice {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String send(String phoneNum) {

        //判断redis中是否有验证码或有效期未过
        String code = redisTemplate.opsForValue().get(phoneNum);
        if (!StringUtils.isEmpty(code)) {
            return code;
        }

        // 如果没有则进行腾讯云短信发送
        //生成6位随机验证码(验证码参数需要数组对象) 可以随意给出6为数字 我这里使用随机数
        String[] params = {RandomUtils.getRandom()};

        try {
            //创建短信发送对象
            SmsSingleSender ssender = new SmsSingleSender(TencentUtils.APPID, TencentUtils.APPKEY);

            SmsSingleSenderResult result = ssender.sendWithParam(TencentUtils.NATIONCODE, phoneNum,
                    TencentUtils.TEMPLATECODE, params, TencentUtils.SIGN, "", "");

            //验证是否发送成功，发送成功将验证码返回 等待用户输入从而进行数据验证
            //{"result":0,"errmsg":"OK","ext":"","sid":"2433:22806866016300548263905129","fee":1,"isocode":"CN"}
            if (result.result == 0) {
                //发送成功 把验证码存入redis 并设置有效期5分钟
                redisTemplate.opsForValue().set(phoneNum, params[0], 5, TimeUnit.MINUTES);
                return params[0];
            }

        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}